import axios from "axios";

export default {
  namespace: 'cinema',
  state: {
    list: []
  },
  reducers: {
    clearList(prev: any, action: any) {
      return {
        ...prev,
        list: []
      }
    },
    changeList(prev: any, action: any) {
      return {
        ...prev,
        list: action.payload.list
      }
    }
  },
  effects: {
    *getList(action: any, obj: any): any {
      // console.log(action, '----------');
      const { put, call } = obj
      // call用于调用异步逻辑，支持 promise 。
      const result = yield call(() => getListAsync(action.payload.cityId))
      // console.log(result);
      //put用于触发 action 。类似dispatch
      yield put({
        type: 'changeList',
        payload: { list: result.data.data.cinemas }
      })
    }
  }
}
async function getListAsync(id: string) {
  const result = await axios({
    method: 'GET',
    url: `https://m.maizuo.com/gateway?cityId=${id}&ticketFlag=1&k=6430675`,
    headers: {
      "X-Client-Info": '{"a":"3000","ch":"1002","v":"5.2.1","e":"16801628172267442084577281"}',
      'X-Host': 'mall.film-ticket.cinema.list'
    }
  })
  return result
}